﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
using System.Text;

namespace Marta.buchalska.wesele
{
    public partial class EntryForm : System.Web.UI.Page
    {
        DataModel.WeedingDBDataContext context = new DataModel.WeedingDBDataContext();


        protected void submitBtn_Click(object sender, EventArgs e)
        {
            List<string> dane = new List<string>();

            GetContaktData(dane);

            foreach (var item in CheckBoxLists)
            {
                dane.Add(item.Value);
            }

            foreach (var item in InputTextList)
            {
                dane.Add(((HtmlInputText)item).Attributes["Title"] + " " + item.Value);
            }

            SendForm(dane);
            Response.Redirect("~/AcceptEntryForm.aspx");
        }

        private void SendForm(List<string> dane)
        {
            StringBuilder sb = new StringBuilder();

            dane.ForEach(d => { sb.AppendLine(d); });

            try
            {
                MailMessage mail = new MailMessage { From = new MailAddress(emailInput.Value) };
                mail.To.Add("kontakt@gardinimontania.pl");

                mail.Subject = "Formularz zgloszeniowy";
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.Body = sb.ToString();
                mail.BodyEncoding = System.Text.Encoding.UTF8;


                SmtpClient smtp = new SmtpClient("mail.gardinimontania.pl")
                {
                    Credentials = new NetworkCredential("kontakt@gardinimontania.pl", "Ok56fap0M3")
                };
                smtp.Send(mail);
            }
            catch (Exception Exception)
            {
                Response.Write(Exception.Message);
            }
        }

        private void GetContaktData(List<string> dane)
        {
            dane.Add("Imię i nazwisko : " + imieInput.Value);
            dane.Add("Miejscowość : " + cityInput.Value);
            dane.Add("Adres e-mail : " + emailInput.Value);

            if (!string.IsNullOrEmpty(phoneContact.Value))
                dane.Add("Telefon kontaktowy : " + phoneContact.Value);

            dane.Add("Data imprezy : " + dataInput.Value);
        }

        List<HtmlInputCheckBox> CheckBoxLists = new List<HtmlInputCheckBox>();
        protected void checkBox_Load(object sender, EventArgs e)
        {
            var t = (System.Web.UI.HtmlControls.HtmlInputCheckBox)sender;
            if (t.Checked)
                CheckBoxLists.Add(t);

        }

        List<HtmlInputText> InputTextList = new List<HtmlInputText>();
        protected void inputText_Load(object sender, EventArgs e)
        {
            if (sender is HtmlInputText)
            {
                var t = (HtmlInputText)sender;
                if (!string.IsNullOrEmpty(t.Value))
                    InputTextList.Add(t);
            }
        }

        protected void tets_binding(object sender, object e)
        {

        }

        protected void TabsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ListViewDataItem dataItem;
            DataModel.Entry_form_item efItem;
            if ((dataItem = e.Item as ListViewDataItem) != null)
            {
                if ((efItem = dataItem.DataItem as DataModel.Entry_form_item) != null)
                {
                    switch (efItem.Entry_form_item_type)
                    {
                        case 1:
                            e.Item.FindControl("checkBoxContent").Visible = true;
                            break;
                        case 2:
                            e.Item.FindControl("textBoxContent").Visible = true;
                            break;
                        case 3:
                            e.Item.FindControl("dateContent").Visible = true;
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}